﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace Twi.COMMON.Core
{
    /// <summary>
    /// 复杂条件对象
    /// </summary>
    public class ComplexModel
    {
        public ComplexModel()
        {
            COperator = CompareOperator.Equals;
            JOperator = JoinOperator.AND;
            DataType = DbType.String;
        }

        /// <summary>
        /// 条件字段
        /// </summary>
        public string Field
        {
            get;
            set;
        }

        /// <summary>
        /// 条件字段值【碰见需要多个值的（如：In、NotIn、BetweenAnd），用逗号隔开。】
        /// </summary>
        public string Value
        {
            get;
            set;
        }

        /// <summary>
        /// 条件比较操作符枚举
        /// </summary>
        public CompareOperator COperator
        {
            get;
            set;
        }

        /// <summary>
        /// 多条件之间连接枚举
        /// </summary>
        public JoinOperator JOperator
        {
            get;
            set;
        }

        /// <summary>
        /// 条件数据类型
        /// </summary>
        /// <remarks>
        /// DataType.Date=5
        /// DataType.DateTime = 6
        /// DataType.Int32 = 11
        /// DataType.String = 16
        /// DataType.Time = 17  
        /// DataType.DateTime2 =26
        /// </remarks>
        public DbType DataType
        {
            get;
            set;
        }

        /// <summary>
        /// 组【多个条件为一组，用括号包含】
        /// </summary>
        public List<ComplexModel> Group
        {
            get;
            set;
        }

        /// <summary>
        /// 根据条件获取生成的原始的SQL条件语句
        /// </summary>
        /// <param name="listCondition"></param>
        /// <returns></returns>
        public static string GetOriginalSQL(List<ComplexModel> listCondition)
        {
            if (listCondition == null || listCondition.Count == 0)
            {
                return null;
            }
            string strSql = "";
            foreach (ComplexModel item in listCondition)
            {
                string sqlItem = item.JOperator == JoinOperator.AND ? " and " : " or ";
                if (item.Group != null && item.Group.Count > 0)
                {
                    string strGroup = GetOriginalSQL(item.Group);
                    sqlItem += " (" + strGroup + ") ";
                    strSql += sqlItem;
                }
                else
                {
                    sqlItem += TwiConditionHelper.GetSimpleConditionSqlItem(item.Field, item.Value, item.COperator);
                    strSql += sqlItem;
                }
            }
            if (strSql.Length > 4)
            {
                strSql = strSql.Substring(4);//把第一个and或or去掉
            }
            return strSql;
        }

    }

    /// <summary>
    /// 条件比较操作符枚举(我这里特意显示写出数字，是方便调用查询数字，免得每次都要数数)
    /// </summary>
    public enum CompareOperator
    {
        Equals = 0,                  //等于
        NotEqualTo = 1,              //不等于
        GreaterThan = 2,             //大于
        LessThan = 3,                //小于
        GreaterThanOrEqualTo = 4,    //大于等于
        LessThanOrEqualTo = 5,       //小于等于
        Like = 6,                    //like '%{0}%'
        StartWith = 7,               //like '{0}%'
        EndWith = 8,                 //like '%{1}'
        In = 9,
        NotIn = 10,
        BetweenAnd = 11
    }

    /// <summary>
    /// 多条件之间连接枚举
    /// </summary>
    public enum JoinOperator
    {
        AND = 0,
        OR = 1
    }

}
